package cn.accp.war;

import java.util.Random;
import java.util.Scanner;

import cn.accp.war.base.Hero;
import cn.accp.war.base.impl.Diaochan;
import cn.accp.war.base.impl.Lvbu;
import cn.accp.war.base.impl.Zhangfei;

public class Game {

	private static Scanner input = new Scanner(System.in);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Random random = new Random();
		int choice;
		do {
			System.out.println("请玩家选择英雄：");
			System.out.println("1.貂蝉");
			System.out.println("2.吕布");
			System.out.println("3.张飞");
			System.out.println("4.游戏退出");
			choice = input.nextInt();
		} while (choice < 1 || choice > 4);
		if (choice == 5) {
			System.out.println("退出了，再见！");
		} else {
			Hero hero = creatHero(choice);
			Hero pc = creatHero(random.nextInt(3) + 1);
			battle(hero, pc);
		}
	}

	public static Hero creatHero(int flag) {
		switch (flag) {
		case 1:
			return new Diaochan();
		case 2:
			return new Lvbu();
		case 3:
			return new Zhangfei();
		default:
			return null;
		}
	}

	public static void battle(Hero hero, Hero pc) {
		Random random = new Random();
		int heroHP = hero.getHealthPoint();
		int pcHp = pc.getHealthPoint();
		int damage = 0;
		while (pcHp > 0 && heroHP > 0) {
			if (gamePRS()) {
				hero.aim();
				hero.move();
				hero.fire();
				damage = random.nextInt(hero.getDamage()) + 1;
				System.out.println(hero.getName() + "给对方造成了" + damage + "点伤害!");
				pcHp -= damage;
			} else {
				pc.aim();
				pc.move();
				pc.fire();
				damage = random.nextInt(pc.getDamage()) + 1;
				System.out.println(pc.getName() + "给对方造成了" + damage + "点伤害!");
				heroHP -= damage;
			}
		}
		if (pcHp < 0) {
			System.out.println(heroHP + "," + pcHp);
			System.out.println("玩家胜!");
		} else {
			System.out.println(heroHP + "," + pcHp);
			System.out.println("电脑胜!");
		}
		input.close();
	}

	public static boolean gamePRS() {
		Random random = new Random();
		boolean flag = false;
		int user, pc;
		do {
			System.out.println("开始石头剪刀布游戏：");
			System.out.println("\t1，石头");
			System.out.println("\t2，剪刀");
			System.out.println("\t3，布");
			System.out.println("请玩家出拳（1-3）：");
			user = input.nextInt();
			pc = random.nextInt(3) + 1;
			PrintChuquan(pc, "电脑");
			PrintChuquan(user, "玩家");
			if (user == pc) {
				System.out.println("\n哎呦，平局，未分胜负，再来一次！");
			} else if (user == 1 && pc == 2 || user == 2 && pc == 3
					|| user == 3 && pc == 1) {
				flag = true;
				System.out.println("\n玩家胜！");
			} else {
				flag = false;
				System.out.println("\n电脑胜!！");
			}
		} while (user == pc);
		return flag;
	}

	public static void PrintChuquan(int chuquan, String name) {
		switch (chuquan) {
		case 1:
			System.out.println(name + "出拳：石头");
			break;
		case 2:
			System.out.println(name + "出拳：剪刀");
			break;
		case 3:
			System.out.println(name + "出拳：布");
			break;
		default:
			System.out.println(name + "乱出拳，视为作弊！");
			break;
		}
	}
}
